<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/22
 * Time: 16:11
 */

namespace app\api\model;


class Task extends BaseModel
{
    public function user(){
//        return $this->belongsTo('User', 'user_id', 'id');
        return $this->belongsTo('User', 'user_id', 'id')->bind('nickname,avatar,level');
//        return $this->belongsTo('User', 'user_id', 'id')->bind(['user_name'=>'nickname','user_avatar'=>'avatar']);
    }

    /**
     * 获取任务列表
     * @param int $page
     * @param int $size
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList_000($page = 1, $size = 20, $cond)
    {
        $cond = [
            'page' => $page,
            'size' => $size,
            'status' => config('status.任务_未完成')
        ];
        $list = $this->where($cond)->field('*')->select();
        return $list;
    }

    public static function getList($where, $page = 1, $size = 20, $order = ['update_time' => 'desc'])
    {
        $res = self::where($where)->with('user')->page($page, $size)->order($order)->select();
//        echo self::getLastSql();
//        $res = collection($res);
        return $res;
//        dump($res);
//        $res = self::all(function ($query) use ($where, $page, $size, $order) {
//            $query->where($where)->order($order)->page($page, $size);
//        });
//        dump($res->());
    }

    /**
     * 获取任务详情
     * @param $task_id
     * @param array $visible
     * @return Task|null
     * @throws \think\exception\DbException
     */
    public static function getOne($task_id, $visible = [])
    {
        return self::get($task_id);
    }

//    //更新数据
//    public static function update($data = [], $where = [], $field = null)
//    {
//        return parent::update($data, $where, $field); // TODO: Change the autogenerated stub
//    }

    //获取字段数据
    public static function getValue($cond, $field)
    {
        return self::where($cond)->value($field);
    }
}